package com.orderdetail.model;

import java.sql.*;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.*;

public class Order_detailDAO implements Order_detailDAO_interface {

	private static DataSource ds;
	private static String INSERT_STMT = "INSERT INTO ORDER_DETAIL (ORD_ID ,GOODSNO ,GOOD_TYPE ,GOOD_PIC ,ORDER_QT ,PRICE) VALUES (? ,? ,? ,? ,? ,?)";

	static {
		try {
			Context ctx = new InitialContext();
			ds = (DataSource) ctx.lookup("java:comp/env/jdbc/ProjectDB");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void insert(Order_detailVO odVO ,Connection conn) {

		Connection con = null;
		PreparedStatement pst = null;
		
		try {
			con = conn;
			pst = con.prepareStatement(INSERT_STMT);
			pst.setLong(1, odVO.getOrd_id());
			pst.setInt(2, odVO.getGoodsno());
			pst.setInt(3, odVO.getGood_type());
			pst.setBytes(4, odVO.getGood_pic());
			pst.setInt(5, odVO.getOrder_qt());
			pst.setInt(6, odVO.getPrice());

			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			if(pst != null)
				try {
					pst.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}

	}
}
